/** 
 * Copyright (C), 2010-2012, AIYOU Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:MidUserAttentionServiceImp.java
 * Version:1.0
 * Date:Dec 8, 2011
 * Description: 
 * author:whh
 */
package com.tietiao.service.miduserattention;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tietiao.common.constants.LogConstants;
import com.tietiao.common.constants.SysUserConstants;
import com.tietiao.dao.MidUserAttentionMapper;
import com.tietiao.dao.model.MidUserAttention;
import com.tietiao.dao.model.MidUserAttentionCriteria;
import com.tietiao.dao.model.SysMessage;
import com.tietiao.dao.model.SysUser;
import com.tietiao.service.syslog.SysLogService;
import com.tietiao.service.sysmessage.SysMessageService;
import com.tietiao.service.sysuser.SysUserService;
@Service
public class MidUserAttentionServiceImp implements MidUserAttentionService {
	@Resource
	private MidUserAttentionMapper midUserAttentionMapper;
	@Resource
	private SysLogService sysLogService;
	@Resource
	private SysUserService sysUserService;
	@Resource
	private SysMessageService sysMessageService;
	/* (non-Javadoc)
	 * @see com.tietiao.service.miduserattention.MidUserAttentionService#deleteAttention(com.tietiao.dao.model.SysUser, int)
	 */
	@Transactional
	public int deleteAttention(SysUser user, int attentionUserId) {
//		SysUser sysUser = sysUserService.loadSysUserById(attentionUserId);
//		sysUser.setWasAttenNum(sysUser.getWasAttenNum()-1);
//		sysUserService.updateSysUser(sysUser);
//		
//		// 处理关注数量-1
//		user = sysUserService.loadSysUserById(user.getUserId());
//		user.setAttenNum(user.getAttenNum()-1);
//		sysUserService.updateSysUser(user);
//		
//		//处理被关注人数量-1
//		SysUser  attentionUser = sysUserService.loadSysUserById(attentionUserId);
//		attentionUser.setWasAttenNum(attentionUser.getWasAttenNum()-1);
//		sysUserService.updateSysUser(attentionUser);
		
		sysLogService.insertSysLog(LogConstants.OP_TYPE_DELETE, "取消关注好友", "好友Id号:"+attentionUserId);
		
		MidUserAttentionCriteria midUserAttentionCriteria = new MidUserAttentionCriteria();
		MidUserAttentionCriteria.Criteria criteria = midUserAttentionCriteria.createCriteria();
		criteria.andUserIdEqualTo(user.getUserId());
		criteria.andSysUserIdEqualTo(attentionUserId);
		return midUserAttentionMapper.deleteByExample(midUserAttentionCriteria);
	}

	/* (non-Javadoc)
	 * @see com.tietiao.service.miduserattention.MidUserAttentionService#getMidMessageUser(com.tietiao.dao.model.SysUser, int)
	 */
	@Override
	public MidUserAttention getMidUserAttention(SysUser user, int attentionUserId) {
		
		MidUserAttentionCriteria midUserAttentionCriteria = new MidUserAttentionCriteria();
		MidUserAttentionCriteria.Criteria criteria = midUserAttentionCriteria.createCriteria();
		criteria.andUserIdEqualTo(user.getUserId());
		criteria.andSysUserIdEqualTo(attentionUserId);
		List<MidUserAttention> list = midUserAttentionMapper.selectByExample(midUserAttentionCriteria);
		
		if(list==null&&list.size()<1){
			return null;
		}else{
			return list.get(0);
		}
	}

	/* (non-Javadoc)
	 * @see com.tietiao.service.miduserattention.MidUserAttentionService#insertMidUserAttention(com.tietiao.dao.model.MidUserAttention)
	 */
	@Transactional
	public int insertMidUserAttention(MidUserAttention midAttention) {
		
		MidUserAttentionCriteria midUserAttentionCriteria = new MidUserAttentionCriteria();
		MidUserAttentionCriteria.Criteria criteria = midUserAttentionCriteria.createCriteria();
		criteria.andUserIdEqualTo(midAttention.getUserId());
		criteria.andSysUserIdEqualTo(midAttention.getSysUserId());
		
		List<MidUserAttention> list = midUserAttentionMapper.selectByExample(midUserAttentionCriteria);
		if(list==null || list.size()==0){
			sysLogService.insertSysLog(LogConstants.OP_TYPE_INSERT, "关注好友", "好友Id号:"+midAttention.getSysUserId());
			SysUser sysUser = sysUserService.loadSysUserById(midAttention.getUserId());
			
			SysMessage sysMessage  = new SysMessage();
			sysMessage.setMessageType(SysUserConstants.MESSAGE_TYPE_ATTENTION_ME);
			sysMessage.setUserId(midAttention.getUserId());
			sysMessage.setMessage(sysUser.getPetName()+"关注您！快去查看他的信息，和他互动吧！");
			sysMessage.setIsValid(1);
			sysMessage.setCreateTime(new Date());
			sysMessageService.addSysMessage(sysMessage, new String[]{midAttention.getSysUserId()+""});
			
			return midUserAttentionMapper.insert(midAttention);
		}else{
			return 1;
		}
		
//		// 处理关注数量+1
//		SysUser user = sysUserService.loadSysUserById(midAttention.getUserId());
//		user.setAttenNum(user.getAttenNum()+1);
//		sysUserService.updateSysUser(user);
//		
//		//处理被关注人数量+1
//		SysUser  attentionUser = sysUserService.loadSysUserById(midAttention.getSysUserId());
//		attentionUser.setWasAttenNum(attentionUser.getWasAttenNum()+1);
//		sysUserService.updateSysUser(attentionUser);
//		
//		sysLogService.insertSysLog(LogConstants.OP_TYPE_INSERT, "关注好友", "好友Id号:"+midAttention.getSysUserId());
		
	}

	/* (non-Javadoc)
	 * @see com.tietiao.service.miduserattention.MidUserAttentionService#updateUserInvite(com.tietiao.dao.model.UserInvite)
	 */
	
	@Transactional
	public int updateUserAttention(MidUserAttention midUserAttention){
		sysLogService.insertSysLog(LogConstants.OP_TYPE_UPDATE, "更新关注好友", "好友Id号:"+midUserAttention.getSysUserId());
		
		MidUserAttentionCriteria midUserAttentionCriteria = new MidUserAttentionCriteria();
		MidUserAttentionCriteria.Criteria criteria = midUserAttentionCriteria.createCriteria();
		criteria.andUserIdEqualTo(midUserAttention.getUserId());
		criteria.andSysUserIdEqualTo(midUserAttention.getSysUserId());
		return midUserAttentionMapper.updateByExample(midUserAttention, midUserAttentionCriteria);
	}

	/* (non-Javadoc)
	 * @see com.tietiao.service.miduserattention.MidUserAttentionService#modifyUserAttentionToDefaultGroup(int)
	 */
	@Override
	public int modifyUserAttentionToDefaultGroup(int groupId) {
		return midUserAttentionMapper.modifyUserAttentionToDefaultGroup(groupId);
	}

	/* (non-Javadoc)
	 * @see com.tietiao.service.miduserattention.MidUserAttentionService#selectFriendsUserCount(java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<String> selectFriendsUser(Integer userId) {
		List<String> list = new ArrayList<String>();
		MidUserAttentionCriteria midUserAttentionCriteria = new MidUserAttentionCriteria();
		MidUserAttentionCriteria.Criteria criteria = midUserAttentionCriteria.createCriteria();
		criteria.andUserIdEqualTo(userId);
		midUserAttentionCriteria.or().andSysUserIdEqualTo(userId);
		
		StringBuffer attenMe = new StringBuffer(",");
		StringBuffer meAtten = new StringBuffer(",");
		List<MidUserAttention> result = midUserAttentionMapper.selectByExample(midUserAttentionCriteria);
		if(result!=null){
			for(MidUserAttention midUserAttention : result) {
				if(userId.equals(midUserAttention.getUserId())){
					meAtten.append(midUserAttention.getSysUserId()).append(",");
				}else{
					attenMe.append(midUserAttention.getUserId()).append(",");
				}
			}
		}
		list.add(attenMe.toString());
		list.add(meAtten.toString());
		return list; 
	}
}
